package com.xy.compose.login.view

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.Alignment
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import com.xy.compose.resource_libs.FontHintColor
import com.xy.compose.resource_libs.FontInputTitleColor
import com.xy.compose.resource_libs.FontWhiteColor
import com.xy.compose.resource_libs.R
import com.xy.compose.resource_libs.ThemeBackground
import com.xy.compose.resource_libs.ThemeButtonUnBackgroundColor

@Composable
fun LoginTop(confirmBackground: Color = ThemeButtonUnBackgroundColor,fontColor: Color = FontWhiteColor, confirmClickable:Boolean = false, backClick:(()-> Unit), confirmClick:(()-> Unit)){
    Box(modifier = Modifier.fillMaxWidth()) {
        Image(
            modifier = Modifier.padding(start = 20.dp)
                .size(48.dp)
                .align(alignment = Alignment.CenterStart).clickable(enabled = true, onClick = backClick),
            painter = painterResource(R.mipmap.icon_login_arrow_left),
            contentDescription = ""
        )
        Text(
            "Confirm",
            modifier = Modifier.padding(end = 20.dp)
                .noRippleClickable(enabled = confirmClickable, onClick = confirmClick)
                .align(alignment = Alignment.CenterEnd)
                .background(
                    confirmBackground,
                    RoundedCornerShape(15.dp)
                )
                .padding(vertical = 3.dp, horizontal = 12.dp),
            textAlign = TextAlign.Center, color = fontColor, fontSize = 20.sp,
        )
    }
}


@Composable
fun ImageBackgroundInput(top: Dp, title:String, textState: TextFieldValue,
                         onTextChange: (TextFieldValue) -> Unit) {
    Box(
        modifier = Modifier.padding(top = top)
            .fillMaxWidth()

    ) {
        // 背景图片
        Image(
            painter = painterResource(id = R.mipmap.icon_edit_bg),
            contentDescription = "输入框背景",
            modifier = Modifier.padding(top = 4.dp).fillMaxWidth().aspectRatio(414f/145f),
            contentScale = ContentScale.Crop // 图片缩放模式，根据需要调整
        )

        // 输入框
        BasicTextField(
            value = textState,
            onValueChange = { onTextChange(it) },
            modifier = Modifier
                .padding(top = 4.dp)
                .fillMaxWidth()
                .padding(horizontal = 41.dp)
                .align(alignment = Alignment.CenterStart),
            textStyle = MaterialTheme.typography.bodyLarge.copy(
                color = FontHintColor,
                fontSize = 16.sp
            ),
            singleLine = true,
            // 当输入为空时显示提示文本
            decorationBox = { innerTextField ->
                if (textState.text.isEmpty()) {
                    Text(
                        text = "Enter",
                        color = FontHintColor,
                        style = MaterialTheme.typography.bodyLarge.copy(
                            fontSize = 16.sp
                        )
                    )
                }
                innerTextField()
            }
        )
        Text(
            text = title,
            color = FontInputTitleColor,
            modifier = Modifier.padding(start = 20.dp),
            fontSize = 20.sp
        )

    }
}

@Composable
fun SignEditor(usernameState: TextFieldValue,
                 onUsernameChange: (TextFieldValue) -> Unit,
                 passwordState: TextFieldValue,
                 onPasswordChange: (TextFieldValue) -> Unit){
    Box(modifier = Modifier.fillMaxWidth().background(ThemeBackground)){

        ImageBackgroundInput(top = 49.dp, title = "Username",usernameState,onUsernameChange)

        ImageBackgroundInput(top = 185.dp, title = "Password",passwordState,onPasswordChange)

    }
}